﻿using MediatR;
using T.ApprovalRecord;
using T.Materials.API.Application.Command.ExamineAndApproveCommand;
using T.Materials.Domain.Model;
using T.Materials.Repository.MaterialsInterface;
using static T.Materials.Domain.Enum.Enums;

namespace T.Materials.API.Application.CommandHandler.ExamineAndApproveCommandHandler
{
    /// <summary>
    /// 审批命令处理器
    /// </summary>
    public class MaterialRequisitionApproveCommandHandler : IRequestHandler<MaterialRequisitionApproveCommand, int>
    {
        /// <summary>
        /// 物资审批接口
        /// </summary>
        protected readonly IMApprow mApprow;

        /// <summary>
        /// 物资领用仓储接口
        /// </summary>
        protected readonly IMaterialRequisitionRepository materialRequisitionRepository;

        /// <summary>
        /// 审批记录数据仓储接口
        /// </summary>
        protected readonly IApprovalRecorddRepository approvalRecorddRepository;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="mApprow">物资审批接口</param>
        /// <param name="materialRequisitionRepository">物资领用仓储接口</param>
        /// <param name="approvalRecorddRepository">审批记录数据仓储接口</param>
        public MaterialRequisitionApproveCommandHandler(IMApprow mApprow, IMaterialRequisitionRepository materialRequisitionRepository, IApprovalRecorddRepository approvalRecorddRepository)
        {
            this.mApprow = mApprow;
            this.materialRequisitionRepository = materialRequisitionRepository;
            this.approvalRecorddRepository = approvalRecorddRepository;
        }

        /// <summary>
        /// 处理审批命令
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<int> Handle(MaterialRequisitionApproveCommand request, CancellationToken cancellationToken)
        {
            var sss = await materialRequisitionRepository.GetModelAsync((int)request.MaterialRequisitionId);
            if (request.ApprovalResult == "同意")
            {
                await mApprow.AddAsync(request);
                sss.ApprovalStatus = ApprovalStatus.已通过;
                sss.Approver = request.Approver;
                await materialRequisitionRepository.UpdateAsync(sss);

                //// 审批记录
                //var approvalRecord = new ApprovalRecordd()
                //{
                //    CompanyID = sss.CompanyID,
                //    User_Id = sss.User_Id,
                //    ApprovalResult = "通过",
                //    ApprovalNotes = "-",
                //    Approver = request.Approver,
                //    MaterialRequisitionId = request.MaterialRequisitionId,
                //};
                

            }
            if (request.ApprovalResult == "同意并转批")
            {
                await mApprow.AddAsync(request);
            }

            if (request.ApprovalResult == "拒绝")
            {
                await mApprow.AddAsync(request);
                sss.ApprovalStatus = ApprovalStatus.已拒绝;
                sss.Approver = request.Approver;
                await materialRequisitionRepository.UpdateAsync(sss);
            }
            return 1;
        }
    }
}
